package com.hcqc.demo.server;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcqc.demo.mapper.Form1Mapper;
import com.hcqc.demo.mapper.Form2Mapper;
import com.hcqc.demo.pojo.Form1;
import com.hcqc.demo.pojo.Form2;
import com.hcqc.demo.vo.DateUtil;
import com.hcqc.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class Form2ServerImpl implements Form2Server {
    @Autowired
    Form2Mapper form2Mapper;

    @Override
    public List<Form2> SelectALL(String user_name) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("DRR", user_name);
        List<Form2> list = form2Mapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<Form2> SelectALL(String user_name, Form2 search) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("DRR", user_name);
        if (search!=null){
            //条件查询
            if (search.getNAME()!="" && search.getNAME()!=null)
                queryWrapper.eq("NAME", search.getNAME());
            if (search.getAGE()!="" && search.getAGE()!=null)
                queryWrapper.eq("AGE", search.getAGE());
            if (search.getMINZHU()!="" && search.getMINZHU() !=null)
                queryWrapper.eq("MINZHU", search.getMINZHU());
        }
        List<Form2> list = form2Mapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public void addFrom2Info(List<Form2> form2List) {
        for (Form2 form2:form2List)
            form2Mapper.insert(form2);
    }

    @Override
    public void addFrom2Info(List<Form2> form2List, String user_name) {
        //添加导入人和导入时间
        for (Form2 form:form2List){
            form.setDRR((String) user_name).setDRSJ(DateUtil.GetNow());
        }
        for (Form2 form2:form2List)
            form2Mapper.insert(form2);
    }

    @Override
    public void delete(String IDs) {
        String[] split = IDs.split(",");
        List<Integer> ids = new ArrayList<>();
        for(String s:split){
            ids.add(Integer.valueOf(s));
        }
        form2Mapper.deleteBatchIds(ids);
    }

    @Override
    public void addOne(Form2 form2) {
        form2Mapper.insert(form2);
    }

    @Override
    public List<Form2> SelectIDs(String user_name, String iDs) {
        System.out.println(iDs);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("DRR", user_name);
        queryWrapper.inSql("ID", iDs);
        List<Form2> list = form2Mapper.selectList(queryWrapper);
        return list;
    }

    //查询所有 条件+分页
    @Override
    public Result SelectALLpage(String user_name, Form2 search, Integer pageIndex, Integer pageSize) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("DRR", user_name);
        if (search!=null){
            //条件查询
            if (search.getNAME()!="" && search.getNAME()!=null)
                queryWrapper.eq("NAME", search.getNAME());
            if (search.getAGE()!="" && search.getAGE()!=null)
                queryWrapper.eq("AGE", search.getAGE());
            if (search.getMINZHU()!="" && search.getMINZHU() !=null)
                queryWrapper.eq("MINZHU", search.getMINZHU());
        }
        //1.通过分页对象 将page rows 进行参数封装
        Page page = new Page(pageIndex,pageSize);
        IPage iPage = form2Mapper.selectPage(page,queryWrapper);
        long total = iPage.getTotal();
        List<Form2> list = iPage.getRecords();	//获取当前页的记录
//        List<Form2> list = form2Mapper.selectList(queryWrapper);
        System.out.println(list);
        return Result.success("成功", list, (int) total);
    }
}
